//
//  SortTagsView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/6/29.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SortTagsView.h"

@implementation SortTagsView
{
    UIImageView * tatalImg;
    UIImageView * varImg;
    UIImageView * screenImg;
    UILabel * totalLabel;
    UILabel * varLabel;
    UILabel * screenLabel;
    BOOL isIS;
}


-(void)creatSortTagsView:(NSArray *)array{
    for (UIView* subView in self.subviews) {
        [subView removeFromSuperview];
    }
    self.dic=[NSMutableDictionary dictionary];
    if (array.count==0) {
        [self creatOnlyTwoSortView];
    }else{
        [self creatVariableSortView:array];
    }
    
    
}
-(void)creatOnlyTwoSortView{
    NSString*str=@"综合";
    CGSize size=[str sizeWithAttributes:@{NSFontAttributeName:FontAndStyle14}];
    CGFloat width = (MainW)/2;
    CGFloat height = 40;
    
    CGFloat short_X = (size.width);
    //CGFloat spacing = (MainW-(size.width+10)*4-20)/3+size.width+10;
    self.totleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [self addSubview:self.totleBtn];
    totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(width/2-size.width/2-8, 12.5, size.width, 15)];
    totalLabel.text= @"综合";
    //oldLabel.backgroundColor=[UIColor redColor];
    totalLabel.textColor=[UIColor redColor];
    totalLabel.font = FontAndStyle12;
    [self addSubview:totalLabel];
    tatalImg = [[UIImageView alloc] initWithFrame:CGRectMake(totalLabel.bsg_x+short_X, totalLabel.bsg_centerY-2.5, 8,5)];
    tatalImg.image = [UIImage imageNamed:@"composite_arrow"];
    self.totalImg=tatalImg;
    [self addSubview:tatalImg];
    //筛选
    screenLabel =[[UILabel alloc] initWithFrame:CGRectMake(width+width/2-size.width/2+8, 12.5, size.width, 15)];
    screenLabel.font =FontAndStyle12;
    screenLabel.text = @"筛选";
    screenLabel.textColor = TextColor3;
    [self addSubview:screenLabel];
    screenImg =[[UIImageView alloc] initWithFrame:CGRectMake(screenLabel.bsg_x+short_X ,screenLabel.bsg_centerY-5, 8, 10)];
    screenImg.image = [UIImage imageNamed:@"screen_nor"];
    self.screenImag=screenImg;
    self.screenlabel=screenLabel;
    [self addSubview:screenImg];
    self.totleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
    self.totleBtn.tag=1;
    [self addSubview:self.totleBtn];
    self.screenBtn = [[UIButton alloc]initWithFrame:CGRectMake(width, 0, width, 40)];
    self.totleBtn.tag=10;
    [self addSubview:self.screenBtn];
//    self.totleBtn.backgroundColor =[UIColor whiteColor];
//    self.screenBtn.backgroundColor =[UIColor redColor];
    [self.totleBtn addTarget:self action:@selector(totalbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.screenBtn addTarget:self action:@selector(screenBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)creatVariableSortView:(NSArray *)array{
    //NSInteger count=array.count;
    self.sortArray=array;
    NSMutableArray *name=[NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSString *string=dic[@"name"];
        [name addObject:string];
    }
    NSString*str=@"综合";
    CGSize size=[str sizeWithAttributes:@{NSFontAttributeName:FontAndStyle14}];
    CGFloat width = (MainW)/(2+array.count);
    CGFloat height = 40;
    
    CGFloat short_X = (size.width);
    self.totleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [self addSubview:self.totleBtn];
    totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(width/2-size.width/2-8, 12.5, size.width, 15)];
    totalLabel.text= @"综合";
    //oldLabel.backgroundColor=[UIColor redColor];
    totalLabel.textColor=[UIColor redColor];
    totalLabel.font = FontAndStyle12;
    [self addSubview:totalLabel];
    tatalImg = [[UIImageView alloc] initWithFrame:CGRectMake(totalLabel.bsg_x+short_X, totalLabel.bsg_centerY-2.5, 8,5)];
    tatalImg.image = [UIImage imageNamed:@"composite_arrow"];
    self.totalImg=tatalImg;
    [self addSubview:tatalImg];
    
    for (int i=0; i<array.count; i++) {
        self.varBtn = [[ZJSortBtn alloc] initWithFrame:CGRectMake(width+width*i, 0, width, 40)];
        self.varBtn.tag=i;
        self.varBtn.varLabel.text=name[i];
        [self addSubview:self.varBtn];
        [self.varBtn addTarget:self action:@selector(varBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
    
    
    //筛选
    screenLabel =[[UILabel alloc] initWithFrame:CGRectMake((MainW-width)+width/2-size.width/2-8, 12.5, size.width, 15)];
    screenLabel.font =FontAndStyle12;
    screenLabel.text = @"筛选";
    screenLabel.textColor = TextColor3;
    [self addSubview:screenLabel];
    screenImg =[[UIImageView alloc] initWithFrame:CGRectMake(screenLabel.bsg_x+short_X ,screenLabel.bsg_centerY-5, 8, 10)];
    screenImg.image = [UIImage imageNamed:@"screen_nor"];
    self.screenImag=screenImg;
    self.screenlabel=screenLabel;
    [self addSubview:screenImg];
    self.totleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, 40)];
    self.totleBtn.tag=1;
    [self addSubview:self.totleBtn];
    self.screenBtn = [[UIButton alloc]initWithFrame:CGRectMake(MainW-width, 0, width, 40)];
    self.totleBtn.tag=10;
    [self addSubview:self.screenBtn];
    //    self.totleBtn.backgroundColor =[UIColor whiteColor];
    //    self.screenBtn.backgroundColor =[UIColor redColor];
    [self.totleBtn addTarget:self action:@selector(totalbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.screenBtn addTarget:self action:@selector(screenBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void)totalbtnClick{
    if (self.isClick==NO) {
        totalLabel.textColor=Red_Color;
        [_delegate firstTotalSortClick];
        tatalImg.image = [UIImage imageNamed:@"composite_arrow"];
        self.isClick=YES;
        varLabel.textColor=TextColor3;
        screenLabel.textColor=TextColor3;
        screenImg.image=[UIImage imageNamed:@"screen_nor"];
        self.lastBtn.varLabel.textColor=TextColor3;
        self.lastBtn.varImage.image=[UIImage imageNamed:@"sales_arrows_nor"];;
        //self.varBtn.selected=NO;
        self.screenBtn.selected=NO;
    }else{
        totalLabel.textColor=Red_Color;
        self.totleBtn.selected=!self.totleBtn.selected;
        if (self.totleBtn.selected) {
            tatalImg.image = [UIImage imageNamed:@"composite_arrow"];
        }else{
            
            tatalImg.image = [UIImage imageNamed:@"composite_arrow_up"];
        }
        //其他按钮的状态
        varLabel.textColor=TextColor3;
        screenLabel.textColor=TextColor3;
        screenImg.image=[UIImage imageNamed:@"screen_nor"];
        [_delegate totalSortClick];
        self.lastBtn.varLabel.textColor=TextColor3;
        self.lastBtn.varImage.image=[UIImage imageNamed:@"sales_arrows_nor"];;
        //self.varBtn.selected=NO;
        self.screenBtn.selected=NO;
    }
    
    
}
-(void)screenBtnClick{
    screenLabel.textColor=Red_Color;
    screenImg.image = [UIImage imageNamed:@"screen_pre"];
    [_delegate screenSortClick];
    self.isClick=NO;
    NSLog(@"这是筛选");
}
-(void)varBtnClick:(ZJSortBtn *)sender{
    self.isClick=NO;
    [self.dic removeAllObjects];
    if (self.lastBtn!=sender) {
        self.lastBtn.varLabel.textColor=TextColor3;
        self.lastBtn.varImage.image=[UIImage imageNamed:@"sales_arrows_nor"];
        self.lastBtn=sender;
        self.lastBtn.selected=NO;
    }
    if (sender.tag==0) {
        self.dic[@"sortType"]=self.sortArray[sender.tag][@"type"];
        sender.selected=!sender.selected;
        if (sender.selected) {
            sender.varImage.image = [UIImage imageNamed:@"sales_arrows_pre_up"];
            self.dic[@"sortMethod"]=@"asc";
        }else{
            
            sender.varImage.image = [UIImage imageNamed:@"sales_arrows_pre_down"];
            self.dic[@"sortMethod"]=@"dasc";
        }
        sender.varLabel.textColor=Red_Color;
       
    }else{
        self.dic[@"sortType"]=self.sortArray[sender.tag][@"type"];
        sender.selected=!sender.selected;
        if (sender.selected) {
            sender.varImage.image = [UIImage imageNamed:@"sales_arrows_pre_up"];
            self.dic[@"sortMethod"]=@"asc";
        }else{
            
            sender.varImage.image = [UIImage imageNamed:@"sales_arrows_pre_down"];
            self.dic[@"sortMethod"]=@"dasc";
        }
        sender.varLabel.textColor=Red_Color;
        //其他按钮的状态
    }
    totalLabel.textColor=TextColor3;
    tatalImg.image=[UIImage imageNamed:@"composite_arrow_nor"];
//    screenLabel.textColor=TextColor3;
//    screenImg.image=[UIImage imageNamed:@"screen_nor"];
    [_delegate varSortClick:self.dic];
    self.totleBtn.selected=NO;
// self.screenBtn.selected=NO;
    
}
@end
